<?php
class Rzd_Validate_UserMarkValues extends Zend_Validate_Abstract
{
   protected $_fieldName;

    const NUM_VALUE_IS_NOT_CORRECT = 'NumValueIsNotCorrect';
    const BOOL_VALUE_IS_NOT_CORRECT = 'BoolValueIsNotCorrect';
    const TYPE_IS_NOT_CORRECT = 'TypeIsNotCorrect';

    protected $_messageTemplates = array(
        self::TYPE_IS_NOT_CORRECT => "Mark type is not corrent",
        self::NUM_VALUE_IS_NOT_CORRECT => 'Numerical values is not correct',
        self::BOOL_VALUE_IS_NOT_CORRECT => 'Boolean values is not correct'
    );

    public function __construct($fieldName) {
        $this->_fieldName = $fieldName;
    }

    public function isValid($value)
    {
        if(!$this->_fieldName) {
           throw new Exception('You haven\'t established the name of a field from which value undertakes.');
        }

        $isValid = true;
        foreach($value as $index => $markTypeValues) {
            foreach($markTypeValues[$this->_fieldName] as $userId => $value) {
                if((int) $markTypeValues['type_id'] !== 1 && (int) $markTypeValues['type_id'] != 2) {
                   $this->_error(self::TYPE_IS_NOT_CORRECT);
                   $isValid = false;
                }

                if((int) $markTypeValues['type_id'] === 1 && (!is_integer($value) || $value > 100)) {
                   $this->_error(self::NUM_VALUE_IS_NOT_CORRECT);
                   $isValid = false;
                } else if((int) $markTypeValues['type_id'] === 2 && ((int) $value !==  1 && (int) $value !== 0)) {
                   $this->_error(self::BOOL_VALUE_IS_NOT_CORRECT);
                   $isValid = false;
                }
            }
        }

        return $isValid;
    }
}